import 'package:flutter/material.dart';
import 'package:hy/common/values/values.dart';
import 'package:hy/modules/home/models/home_list_model.dart';
import 'package:hy/modules/home/pages/home_image_edit_page.dart';
import 'package:hy/modules/home/widget_models/home_list_widget_model.dart';
import 'package:hy/modules/home/widgets/home_list_unit_widget.dart';
import 'package:image_picker/image_picker.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  /// 数据集合
  List<HomeListElementResponseModel> dataArray =
      <HomeListElementResponseModel>[];

  /// 图片选择器
  final ImagePicker _picker = ImagePicker();

  @override
  void initState() {
    super.initState();

    /// 初始加载数据
    _loadData();
  }

  @override
  void dispose() {
    super.dispose();
  }

  /// 加载数据
  Future<void> _loadData() async {
    List<HomeListElementResponseModel> dataModels =
        await HomeListWidgetModel.initializeData();
    setState(() {
      dataArray.addAll(dataModels);
    });
    print("结束---:$dataArray");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        title: Text(
          "Hyperbound Flutter Demo",
          style: TextStyle(
            color: AppColor.colorBlack,
            fontSize: 24,
            fontWeight: FontWeight.w700,
          ),
        ),
      ),
      body: RefreshIndicator(
        onRefresh: _loadData,
        strokeWidth: 4,
        color: AppColor.colorDarkCyan,
        backgroundColor: AppColor.colorBlack,
        child: ListView.separated(
          itemBuilder: (context, index) {
            HomeListElementResponseModel model = dataArray[index];
            return HomeListUnitWidget(responseModel: model);
          },
          separatorBuilder: (BuildContext context, int index) {
            return Divider(
              color: AppColor.colorLineGrey,
              height: 1,
            );
          },
          itemCount: dataArray.length,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          // bool isHasPermission =
          //     await PermissionUtil.requestPermission(context);
          //
          // if (isHasPermission) {
          //   final XFile? image =
          //       await _picker.pickImage(source: ImageSource.gallery);
          //   print("----: ${image?.path}");
          // }

          final XFile? image =
              await _picker.pickImage(source: ImageSource.gallery);
          print("----: ${image?.path}");

          Navigator.of(context).push(MaterialPageRoute(builder: (context) {
            return HomeImageEditPage(imagePath: image?.path ?? "");
          }));
        },
        backgroundColor: Colors.transparent,
        elevation: 0,
        tooltip: 'release',
        child: Container(
          width: 50,
          height: 50,
          decoration: BoxDecoration(
              borderRadius: Radii.k50pxRadius,
              gradient: LinearGradient(
                colors: [AppColor.colorLightCyan, AppColor.colorDarkCyan],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              )),
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}
